<?php
class TPLAdminAdver {
	/*
		Меню модуля
	 */
	protected static function Menu($act='')
	{
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$links=&$GLOBALS['Eleanor']->module['links'];

		$categs=isset($GLOBALS['Eleanor']->module['navigation']['categories']) ? $GLOBALS['Eleanor']->module['navigation']['categories'] : false;
		$options=isset($GLOBALS['Eleanor']->module['navigation']['options']) ? $GLOBALS['Eleanor']->module['navigation']['options'] : false;
		$GLOBALS['Eleanor']->module['navigation']=array(
		array($links['list'],$lang['list'],'act'=>$act=='list',
		'submenu'=>array(
		$links['newlist'] ? array($links['newlist']['link'],sprintf($lang['news'],$links['links']['cnt']),'act'=>false) : false,
		array($links['add'],$lang['add'],'act'=>$act=='add'),
		),
		),
		$options ? $options : array($links['options'],Eleanor::$Language['main']['options'],'act'=>$act=='options'),
		$categs ? $categs : array($links['categories'],$lang['cats_manage']),
		);
	}

	public static function AdverList($items,$cnt,$pp,$query,$page,$sort,$so, $links) {
		static::Menu();
		$GLOBALS['jscripts'][]='js/checkboxes.js';
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$fititlet=$statuses='';
		$query += array(
			'fititlet'=>null,
			'fistatus'=>null,
			'ficategory'=>null,
		);
		$temp=array(
			'b'=>$lang['begins'],
			'q'=>$lang['match'],
			'e'=>$lang['endings'],
			'm'=>$lang['contains'],
		);
		foreach($temp as $k=>&$v)
		$fititlet.=Eleanor::Option($v,$k,$query['fititlet']==$k);
		$temp=array(
		-1=>$lang['waitmod'],
		0=>$lang['blocked'],
		1=>$lang['active'],
		);
		foreach($temp as $k=>&$v)
		$statuses.=Eleanor::Option($v,$k,$query['fistatus']==$k);
		$c='<form method="post">
			<table class="tabstyle tabform" id="ftable">
				<tr class="infolabel"><td colspan="2"><a href="#">'.$lang['filters'].'</a></td></tr>
				<tr>
					<td><b>'.Eleanor::$Language['tpl']['title'].'</b><br />'.Eleanor::Select('fititlet',$fititlet,array('style'=>'width:30%')).Eleanor::Input('fititle',isset($query['fititle']) ? $query['fititle'] : '',array('style'=>'width:68%')).'</td>
					<td><b>'.$lang['category'].'</b><br />'.Eleanor::Select('ficategory',Eleanor::Option('-'.Eleanor::$Language['tpl']['no'].'-',0,!$query['ficategory']).Eleanor::Option($lang['nocat'],'no',$query['ficategory']=='no').$GLOBALS['Eleanor']->Categories->GetOptions($query['ficategory'])).'</td>
				</tr>
				<tr>
					<td><b>'.$lang['status'].'</b><br />'.Eleanor::Select('fistatus',$statuses).'</td>
					<td style="text-align:center;vertical-align:middle">'.Eleanor::Button(Eleanor::$Language['tpl']['apply']).'</td>
				</tr>
			</table>
			<script>//<![CDATA[
				var fitrs=$("#ftable tr:not(.infolabel)");
				function SwitchFilter()
				{
					fitrs.toggle();
					$("#ftable .infolabel a").toggleClass("selected");
				}
				$(function(){
					'.(empty($query['filter']) ? '' : 'fitrs.hide();').'
					SwitchFilter();
					$("#ftable .infolabel a").click(function(){SwitchFilter();return false});
				});
			//]]></script>
		</form><form id="checks-form" action="'.$GLOBALS['Eleanor']->Url->Construct($query+array('page'=>$page)).'" method="post" onsubmit="return (CheckGroup(this) && confirm(\''.Eleanor::$Language['tpl']['are_you_sure'].'\'))">';
		$Lst=Eleanor::LoadListTemplate('table-list',7);
		$c.=$Lst->begin(
		array('id',17,'sort'=>$sort=='id' ? $so : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($query,array('sort'=>'id','so'=>($sort=='id' and $so=='asc') ? 'desc' : 'asc')))),
		array(Eleanor::$Language['tpl']['title'],'sort'=>$sort=='title' ? $so : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($query,array('sort'=>'title','so'=>($sort=='title' and $so=='asc') ? 'desc' : 'asc')))),
		$lang['category'],
		array($lang['date'],'sort'=>$sort=='date' ? $so : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($query,array('sort'=>'date','so'=>($sort=='date' and $so=='asc') ? 'desc' : 'asc')))),
		array($lang['author'],'sort'=>$sort=='author' ? $so : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($query,array('sort'=>'author','so'=>($sort=='author' and $so=='asc') ? 'desc' : 'asc')))),
		array(Eleanor::$Language['tpl']['functs'],80,'sort'=>$sort=='id' ? $so : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($query,array('sort'=>'id','so'=>($sort=='id' and $so=='asc') ? 'desc' : 'asc')))),
		array(Eleanor::Check('mass',false,array('id'=>'mass-check')),20)
		);
		if($items) {
			$editimg=Eleanor::$Template->default['theme'].'images/' . 'edit.png';
			$delimg=Eleanor::$Template->default['theme'].'images/' . ('delete.png');
			$inimg=Eleanor::$Template->default['theme'].'images/' . ('inactive.png');
			$acimg=Eleanor::$Template->default['theme'].'images/' . ('active.png');
			foreach($items as &$arr)
			{
				$cats='';
				foreach($arr['cats'] as &$v)
				if(isset($GLOBALS['Eleanor']->Categories->dump[$v]))
				$cats.=($cats ? '' : '<b>').$GLOBALS['Eleanor']->Categories->dump[$v]['title'].($cats ? '' : '</b>').', ';
				$c.=$Lst->item(
				array('<a name="it'.$arr['id'].'"></a>'.$arr['id'],'right'),
				'<a href="'.($edit=$GLOBALS['Eleanor']->Url->Construct(array('edit'=>$arr['id']))).'">'.$arr['title'].'</a>',
				$cats ? rtrim($cats,', ') : array('--','center'),
				array(($arr['pinned'] and $arr['_pinned']) ? $arr['pinned'] : $arr['date'],'center'),
				'<a href="'.Eleanor::$Login->UserLink($arr['author'],$arr['author_id']).'">'.$arr['author'].'</a>',
				$Lst->func(
				$arr['status']>=0 ? array($GLOBALS['Eleanor']->Url->Construct(array('swap'=>$arr['id'])),$arr['status'] ? Eleanor::$Language['tpl']['deactivate'] : Eleanor::$Language['tpl']['activate'],$arr['status'] ? $acimg : $inimg) : false,
				array($edit,Eleanor::$Language['tpl']['edit'],$editimg),
				array($GLOBALS['Eleanor']->Url->Construct(array('delete'=>$arr['id'])),Eleanor::$Language['tpl']['delete'],$delimg)
				),
				Eleanor::Check('mass[]',false,array('value'=>$arr['id']))
				);
			}
		}
		else
			$c.=$Lst->empty($lang['not_found']);
		$pps='';
		foreach(array(30,50,100,500) as $v)
		$pps.=$v==$pp ? ' '.$v.' |' : ' <a href="'.$GLOBALS['Eleanor']->Url->Construct($query+array('new-pp'=>$v)).'">'.$v.'</a> |';
		$c.=$Lst->end().'<div class="submitline" style="text-align:right"><div style="float:left">'.sprintf($lang['tto_pages'],rtrim($pps,'|')).'</div>'.Eleanor::$Language['tpl']['with_selected'].Eleanor::Select('op',Eleanor::Option(Eleanor::$Language['tpl']['activate'],'a').Eleanor::Option(Eleanor::$Language['tpl']['deactivate'],'d').Eleanor::Option(Eleanor::$Language['tpl']['delete'],'k').Eleanor::Option($lang['waitmod'],'m')).Eleanor::Button('OK').'</div></form><script>/*<![CDATA[*/$(function(){One2AllCheckboxes("#checks-form","#mass-check","mass[]",true)})//]]></script>';
		return Eleanor::$Template->Cover($c.Eleanor::$Template->Pages($cnt,$pp,$page,$query));
	}

	public static function AddEdit($id,$values,$error,$uploader,$bypost) {
		static::Menu();
		if(!isset($GLOBALS['head']['colorbox'])) {
			$GLOBALS['jscripts'][]='addons/colorbox/jquery.colorbox-min.js';
			$GLOBALS['head']['colorbox']='<link rel="stylesheet" media="screen" href="addons/colorbox/colorbox.css">';
		}
		$GLOBALS['head'][]='<script>//<![CDATA[
		$(function(){
			$("a.gallery").colorbox({
					title: function(){
					var url=$(this).attr("href"),
					title=$(this).find("img").attr("title");
					return "<a href=\""+url+"\" target=\"_blank\">"+(title ? title : url)+"</a>";
				},
				maxWidth:Math.round(screen.width/1.5),
				maxHeight:Math.round(screen.height/1.5),
			});
		});//]]></script>';
		$GLOBALS['jscripts'][] = '/modules/adver/js/region.js';
		// $GLOBALS['jscripts'][] = '/modules/adver/js/ext-core.js';#Конфликт
		$GLOBALS['jscripts'][] = '/modules/adver/js/uploader.js';
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];

		$ml=array(
			'meta_title'=>Eleanor::Input('meta_title',$values['meta_title'],array('tabindex'=>15)),
			'meta_descr'=>Eleanor::Input('meta_descr',$values['meta_descr'],array('tabindex'=>16)),
		);
		$g=$goroda='';
		#TODO REmake
		$goroda=array('Liepājas rajons','Rēzeknes rajons','Aizkraukles rajons','Rīgas rajons','Rīga','Aluksnes rajons','Balvu rajons','Bauskas rajons','Cēsu rajons','Daugavpils rajons','Dobeles rajons','Gulbenes rajons','Jēkabpils rajons','Jelgavas rajons','Krāslavas rajons','Kuldīgas rajons','Limbažu rajons','Ludzas rajons','Madonas rajons','Ogres rajons','Preiļu rajons','Saldus rajons','Tukuma rajons','Talsu rajons','Valkas rajons','Valmieras rajons','Ventspils rajons');

		$g = Eleanor::Option($lang['iz'], $lang['iz']);
		sort($goroda);

		foreach($goroda as $go) {
			$g.=Eleanor::Option($go, $go, $go == isset($values['region']));
		}

		$images = '';
		if (isset($values['images']) && $values['images']) {
			foreach ($values['images'] as $v) {
				$images .= '<img style="margin: 5px" src="'. $v .'" alt ="">';
			}
		}
		$Lst = Eleanor::LoadListTemplate('table-form');
		$c = '<form method="post">'
		.$Lst->begin()
		.$Lst->item(Eleanor::$Language['tpl']['title'],Eleanor::Input('title',$values['title']))
		.($GLOBALS['Eleanor']->Categories->dump
		? $Lst->item($lang['categs'],Eleanor::Items('cats',$GLOBALS['Eleanor']->Categories->GetOptions($values['cats']),array('id'=>'cs','tabindex'=>2))).
		$Lst->item($lang['maincat'],Eleanor::Select('_tplcat',$GLOBALS['Eleanor']->Categories->GetOptions($values['_maincat']),array('id'=>'mc','tabindex'=>3))):'')
		.$Lst->end();

		$text = $Lst->begin()
		.$Lst->item(array($lang['short_text'],$GLOBALS['Eleanor']->Editor->Area('text',$values['text'],array('bypost'=>$bypost,'no'=>array('tabindex'=>6))),'descr'=>$lang['short_text_']))
		. $Lst->item($lang['status'],Eleanor::Select('status',Eleanor::Option($lang['waitmod'],-1,$values['status']==-1).Eleanor::Option($lang['blocked'],0,$values['status']==0).Eleanor::Option($lang['active'],1,$values['status']==1),array('tabindex'=>9)))
		. $Lst->item(array($lang['na'],Eleanor::Input('name', $values['name'])))
		. $Lst->item(array($lang['ema'],Eleanor::Input('email', $values['email'])))
		. $Lst->item(array($lang['tel'],Eleanor::Input('telefon', $values['telefon'])))
		. $Lst->item(array($lang['webadres'],Eleanor::Input('webadres', $values['webadres'])))
		. $Lst->item($lang['region'], Eleanor::Select('region', $g,array('id' => 'region', 'onLoad'=>'"this.focus = false;"', 'onChange'=>"ajaxLoad('city', '/1/modules/adver/region.php?region='+this.options[this.selectedIndex].value, '','',''); document.getElementById('city').disabled='';document.getElementById('city1').style.display='';")))
		. $Lst->item(array($lang['rajon'], Eleanor::Select('rajon', '',array('id' => 'city','onChange'=>"document.getElementById('region').disabled='';",'disabled'=>'disabled')),'tr'=>array('style'=>'display:none;', 'id'=>'city1')))
		. $Lst->item(array($lang['adres'],Eleanor::Input('adres', $values['adres'])))
		. $Lst->item(array($lang['past'],Eleanor::Input('pasta_index', $values['pasta_index'])))
		. $Lst->item(array($lang['pr'],Eleanor::Input('price', $values['price'])))
		. $Lst->item($lang['data_end'],Dates::Calendar('date_end',$values['date_end'],true,array('tabindex'=>12)))
		#TODO SHOW pic
		. $Lst->item('Images', $images)
		. $Lst->end();

		$extra=$Lst->begin()
		.$Lst->item('Uri',Eleanor::Input('uri',$values['uri'],array('onfocus'=>'if(!$(this).val())$(this).val($(\'#title\').val())','tabindex'=>10)))
		.$Lst->item($lang['author'],Eleanor::$Template->Author($values['author'],$values['author_id'],11))
		.$Lst->item(array($lang['pdate'],Dates::Calendar('date',$values['date'],true,array('tabindex'=>12)),'tip'=>$lang['pdate_']))
		.$Lst->item($lang['pinned'],Dates::Calendar('pinned',$values['pinned'],true,array('tabindex'=>13)))
		.$Lst->item($lang['reads'],Eleanor::Input('reads',$values['reads'],array('tabindex'=>14)))
		.$Lst->item('Window title',Eleanor::$Template->LangEdit($ml['meta_title'],false))
		.$Lst->item('Meta description',Eleanor::$Template->LangEdit($ml['meta_descr'],false))
		.$Lst->end();

		$back = isset($_GET['noback']) ? '' : Eleanor::Input('back',isset($_POST['back']) ? (string)$_POST['back'] : getenv('HTTP_REFERER'),array('type' => 'hidden'));
		$c .= $Lst->tabs(
			array('General', $text),
			array('Settings', $extra)
		)
		// .$Lst->submitline($uploader)
		.$Lst->submitline($back . Eleanor::Button('Ok','submit',array('tabindex'=>20)).($id ? ' '.Eleanor::Button(Eleanor::$Language['tpl']['delete'],'button',array('onclick'=>'window.location=\''.$GLOBALS['Eleanor']->Url->Construct(array('delete'=>$id,'noback'=>1)).'\'')) : ''))
		.$Lst->endform();
		return Eleanor::$Template->Cover($c,$error).'<script>//<![CDATA[
		$(function(){
			$("#cs").change(function(){
				var cs=this;
				$("#mc option").each(function(i){
					if($("option:eq("+i+")",cs).prop("selected"))
						$(this).prop("disabled",false);
					else
						$(this).prop({disabled:true,selected:false});
				});
			}).change();
		})//]]></script>';
	}

	public static function Categories($c)
	{
		static::Menu();
		return$c;
	}
}
?>